package com.uade.services;

import java.io.IOException;
import java.io.StringReader;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.NamingException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.uade.model.Articulo;

import com.uade.xml.ArticuloHandler;

/**
 * Message-Driven Bean implementation class for: ProductoJMS
 * 
 */
@MessageDriven( mappedName = "test", activationConfig = {
		
		@ActivationConfigProperty(propertyName = "clientId", propertyValue = "portalwebClientID"),
		@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/deposito"),
		@ActivationConfigProperty(propertyName = "user", propertyValue = "deposito"),
		@ActivationConfigProperty(propertyName = "password", propertyValue = "deposito123"),
		@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class AltaArticuloJMS implements MessageListener {
	private Logger log = Logger.getLogger(AltaArticuloJMS.class);

	@Override
	public void onMessage(Message msg) {

		try {
			log.info("Inicia el proceso de validacion del mensaje");
			Articulo articulo = parseXml(((TextMessage) msg).getText());
			if (articulo != null) {
				log.info("Invoco al Facade");
				log.info("Envio al facade el alta de articulo para el codigo "
						+ articulo.getIdProductoDeposito());
				ServiceLocator.getRemoteFacadeB().nuevoArticulo(articulo);
			} else
				log.error("No se agrego el Articulo");
			log.info("Finaliza el procesamiento de la cola. Queda a la espera de nuevos mensajes");

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private Articulo parseXml(String xml) {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		ArticuloHandler handler = new ArticuloHandler();
		try {
			SAXParser saxParser = factory.newSAXParser();

			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xml));

			saxParser.parse(is, handler);

		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		if (handler.getArticuloList() != null
				&& handler.getArticuloList().size() > 0)
			return handler.getArticuloList().get(0);
		return null;
	}
}
